package com.khyr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.dao.UserDao;
import com.khyr.entity.Dept;
import com.khyr.entity.User;
import com.khyr.service.UserService;
import com.khyr.vo.R;
import com.khyr.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 宋一恒
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    //密码加密
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    //查询
    public R<Page<User>> findUserByCond(UserVo userVo) {
        //分页
        Page<User> page = new Page<>(userVo.getCurrent(), userVo.getPageSize());
        //模糊
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(userVo.getUserName())) {
            queryWrapper.like("user_name", userVo.getUserName());
        }
        if (StringUtils.hasText(userVo.getStatus())) {
            queryWrapper.eq("status", userVo.getStatus());
        }
        if (StringUtils.hasText(userVo.getPhone())) {
            queryWrapper.eq("phone", userVo.getPhone());
        }
        if (StringUtils.hasText(userVo.getDeptId())) {
            queryWrapper.eq("dept_id", userVo.getDeptId());
        }
        System.out.println(userVo.getBeginTime());
        if (StringUtils.hasText(userVo.getBeginTime())&&StringUtils.hasText(userVo.getEndTime())){
            queryWrapper.between("create_time",userVo.getBeginTime(),userVo.getEndTime());
        }
        Page<User> UserIPage = userDao.selectPage(page, queryWrapper);
        return new R<Page<User>>(200, "查询成功", UserIPage);

    }

    @Override
    //添加
    public R<Integer> addUser(User user) {
        if (userDao.insert(user) > 0) {
            return new R<Integer>(200, "添加成功", 1);
        }
        return new R<Integer>(500, "添加失败", null);
    }

    @Override
    public R<Integer> deleteUserByIds(List<Long> ids) {
        int i = userDao.deleteUserByIds(ids);
        return null;
    }

    @Override
    public R<Integer> updateUser(User user) {
        if (userDao.updateById(user) > 0) {
            return new R<Integer>(200, "修改成功", 1);
        }
        return new R<Integer>(500, "修改失败", null);
    }

    @Override
    public R<User> getUserById(Long id) {
        if (userDao.selectById(id) != null) {
            return new R<User>(200, "查询成功", userDao.selectById(id));
        }
        return new R<>(500, "查询失败", null);
    }

    @Override
    public R<Integer> resetPwd(List<Long> userIds) {
        //密码加密
        String encode = passwordEncoder.encode("123456");
        for (Long userId : userIds) {
            User user = new User();
            user.setUserId(userId);
            user.setPassword(encode);
            userDao.updateById(user);
        }
        return new R<Integer>(200, "重置成功", 1);
    }

    @Override
    public R<Integer> userPhone(String phone) {
        if (userDao.selectOne(new QueryWrapper<User>().eq("phone", phone)) != null) {
            return new R<Integer>(208, "手机号已存在", 1);
        }
        return new R<Integer>(200, "手机号不存在", null);
    }

    @Override
    public R<Integer> resetPwd1(Long userIds, String password) {
        //先将密码加密
        String encode = passwordEncoder.encode(password);
        User user = new User();
        user.setPassword(encode);
        user.setUserId(userIds);
        if (userDao.updateById(user) > 0) {
            return new R<Integer>(200, "重置成功", 1);
        }
        return new R<Integer>(500, "重置失败", null);
    }

    @Override
    public R<String> getdocNameById(Long valueId) {
        if (valueId != null) {
            User user = userDao.selectById(valueId);
            return new R<String>(200, "查询成功",user.getUserName());
        }
        return new R<>(500, "查询失败", null);
    }


}
